package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 360好药
 * @author tngou@tngou.net
 *
 */
public class COM_360haoyao extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_360haoyao.class);

	private String url="http://www.360haoyao.com/item/${}.html";
	private String purl="http://origin.360haoyao.com/nitem/getItemInfo.action";
	
	

	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null) return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("/");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements detailnav = doc.getElementsByClass("detailnav");
		if(detailnav.first()!=null)
		{
			Elements as = detailnav.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size(); i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		//图片
		Element thumblist = doc.getElementById("thumblist");
		if(thumblist!=null)
		{
			Elements as = thumblist.getElementsByTag("a");
			List<String> list = new ArrayList<>();
			as.forEach(e->{
				String rel = e.attr("rel");
				if(StringUtil.isNotEmpty(rel))
				{
					JSONObject js=(JSONObject) JSON.parse(rel);
					if(js!=null)
					{
						String largeimage = js.getString("largeimage");
						if(StringUtil.isNotEmpty(largeimage))
						{
							list.add(largeimage);
						}
					}
				}				
			});
					json.put("picture", list);
		}
		//价格
		json.put("price", getPrice(json.getString("un")));
		
		//类型
		Elements dtlInfoMid = doc.getElementsByClass("dtlInfoMid");
		if(dtlInfoMid.first()!=null)
		{
			Elements h1 = dtlInfoMid.first().getElementsByTag("h1");
			if(h1.first()!=null)
			{
				String s = h1.first().getElementsByTag("i").toString();
				if(StringUtil.isNotEmpty(s))
				{
					if(s.toUpperCase().indexOf("RX")>=0)
					{
						json.put("ptype", Ptype.RX处方药.ordinal());
					}if(s.toUpperCase().indexOf("OTC")>=0)
					{
						json.put("ptype", Ptype.OTC非处方甲类.ordinal());
					
					}
				}
			}
			
		}
		//说明书
		//说明书
				Elements instruction = doc.getElementsByClass("desTable");
				if(instruction.first()!=null)
				{
					JSONObject instructions = instructions_tr_th_td(instruction.first());		
					json.put("instructions",instructions );
				
				}
	
		return json;
	}

	public float getPrice(String itemid) {
		String rs = HttpUtil.getString(purl, new HashMap<String,String>() {{put("itemId", itemid);}});
		if(rs!=null)
		{	
			JSONObject rjs = (JSONObject) JSON.parse(rs);
			
			if(rjs!=null) {
				JSONObject item=rjs.getJSONObject("item");
				if(item!=null)
					return item.getFloatValue("originalPrice");
			}		
		}
		return 0;
	}
	
}
